package com.hboxs.asl.controller.app.product;

import com.hboxs.asl.Constant;
import com.hboxs.asl.JsonMessage;
import com.hboxs.asl.Page;
import com.hboxs.asl.Pageable;
import com.hboxs.asl.controller.app.BaseController;
import com.hboxs.asl.entity.Ad;
import com.hboxs.asl.entity.Member;
import com.hboxs.asl.entity.Question;
import com.hboxs.asl.entity.Review;
import com.hboxs.asl.service.AdService;
import com.hboxs.asl.service.MemberService;
import com.hboxs.asl.service.QuestionService;
import com.hboxs.asl.service.ReviewService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Controller(value = "appQuestionControl")
@RequestMapping(value = "/app/question")
public class QuestionControl extends BaseController {


    @Resource(name = "memberServiceImpl")
    private MemberService memberService;
    @Resource(name = "questionServiceImpl")
    private QuestionService questionService;
    @Resource(name = "adServiceImpl")
    private AdService adService;
    @Resource(name = "reviewServiceImpl")
    private ReviewService reviewService;

    /**
     * 易问易答列表
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public
    @ResponseBody
    JsonMessage list(Pageable pageable) {
        try {

            List<Ad> ads = adService.findListByAdPosition(Constant.WECHAT_AD_POSITION_BANNER);

            Page<Question> page = questionService.findPageByAnswer(pageable);

            return JsonMessage.success(

                    "ads", JsonMessage.optimiseObj(ads, "id", "path", "url", "teacherId"),

                    "totalPage", page.getTotalPages(),

                    "pageNumber", page.getPageNumber(),

                    "pageSize", page.getPageSize(),

                    "total", page.getTotal(),

                    "content", JsonMessage.optimiseObj(

                            page.getContent(), "title", "id", "createDateF", "content", "mediaURL", "teacher.avator", "teacher.name", "teacher.id", "images", "timeLength",

                            "member.username", "name", "member.name", "payPrice", "payType", "birthDay", "birthHour", "gender", "email"

                    ));

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }

    /**
     * 易问易答列表（按老师id）
     * @param pageable
     * @param teacherId
     * @return
     */
    @RequestMapping(value = "/questionList", method = RequestMethod.GET)
    public
    @ResponseBody
    JsonMessage questionList(Pageable pageable, Long teacherId) {
        try {
            Member teacher = memberService.find(teacherId);
            if(teacher == null) {
                return JsonMessage.error("找不到该老师");
            }
            Page<Question> questionPage = questionService.findQuestionByTeacher(pageable, teacher, Question.PaymentStatus.paid, Question.AnswerStatus.yes);
            return JsonMessage.success(
                    "totalPage", questionPage.getTotalPages(),
                    "pageNumber", questionPage.getPageNumber(),
                    "pageSize", questionPage.getPageSize(),
                    "total", questionPage.getTotal(),
                    "content", JsonMessage.optimiseObj(
                            questionPage.getContent(),
                            "title", "answerContent", "id", "createDateF", "mediaURL", "images", "content", "teacher.avator", "teacher.name", "timeLength",
                            "member.username", "name", "member.name", "payPrice", "payType", "birthDay", "birthHour", "gender", "email"
                            ));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }


    /**
     * 问答详情
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/answerWeb", method = RequestMethod.GET)
    public
    String detail(Long id, ModelMap modelMap) {
        Question question = questionService.find(id);
        if (question == null) {
            return "/yijing/404";
        }
        modelMap.addAttribute("answer" , question);
        return "/yijing/answerWeb";
    }
}
